#ifndef __MISSILEACTOR_H__
#define __MISSILEACTOR_H__

#include <math.h>
#include <set>
#include "actor.h"

using std::set;

// A missile
class MissileActor : public Actor
{
public:
  enum Type {ENEMY, PLAYER};

public:
  MissileActor(f32 xi, f32 yi, f32 xf, f32 yf, Type owner, int splitafter=-1);

  bool isExploded() {return state == MS_EXPLODE;}
  void explode();
  void notifyExploded();

  bool shouldSplit() {return state == MS_SPLITME;}
  void split(int n);
  bool isSplit() {return state == MS_DIDSPLIT;}
  void setSplitParent(MissileActor *parent) {splitParent = parent;}

  void finish() {state = MS_FINISHED;}
  bool isFinished() {return state == MS_FINISHED || state == MS_EXPLODE;}

  f32 getTargetX() {return xf;}
  f32 getTargetY() {return yf;}
  f32 getStartX() {return xi;}
  f32 getStartY() {return yi;}

  MissileActor::Type getOwner() {return owner;}

public:
  void activate();
  void update();
  
  f32 getX() {return x;}
  f32 getY() {return y;}
  f32 getR() {return MRAD;}
  f32 getRotation() {return theta*180.0/M_PI;}
  int getT() {return t;}
  bool isActive() {return state != MS_NULL && state != MS_FINISHED;}

public:
  Sprite missileTarget;
  Sprite missileCircle;
  Sprite missileLine;

protected:
  const static f32 MSPEED = 3.0;
  const static f32 ENEMYSPEED = 1.0;
  const static f32 MRAD = 1.0;

  enum {
    MS_NULL,
    MS_FLIGHT,  // A missile en route to its target
    MS_EXPLODE, // An exploded missile needs to be swapped for a shockwave
    MS_SPLITME, // A missile that has split into submissiles
    MS_DIDSPLIT,
    MS_FINISHED
  } state;

  f32 xi,yi; // initial coordinates
  f32 xf,yf; // target coordinates
  f32 x,y; // current coordinates
  f32 theta; // orientation
  f32 d; // distance traveled since xi,yi
  f32 maxd; // total distance from xi,yi to xf,yf
  int t;
  Type owner; // who fired the missile
  int splitCount;
  int splitTime;
  MissileActor *splitParent;
};

#endif
